
# Create your views here.
from djsite.pages.models import Page
from djsite.menu.models import Hor, Ver
from django.shortcuts import render_to_response
from django.http import HttpResponseRedirect
from django.contrib.auth.decorators import login_required

def view_page(request, page_name):
	user = request.user 	
    	try:
		html = Page.objects.get(code=page_name)
		hor = Hor.objects.all()
		ver = Ver.objects.all()
	except Page.DoesNotExist:
         	return render_to_response("page_add.html", {
 "page_name":page_name,
 "user":user
 })	
	return render_to_response("page_view.html", {
 "page_name":page_name,
 "html": html,
 "hor":hor,
 "ver":ver,
 "user":user
 })
   
@login_required
def edit_page(request, page_name):
	try:
		page = Page.objects.get(code=page_name)
		code = page.code
		title = page.title
		content = page.content
	except Page.DoesNotExist:
		code = page_name
		title = ""
		content = ""
	return render_to_response("page_edit.html", {
 "page_name":page_name,
 "code":code,
 "title":title,
 "content":content,
 })

@login_required
def save_page(request, page_name):
        code = request.POST["code"]
        title = request.POST["title"]
	content = request.POST["content"]
	try:
		page = Page.objects.get(code=page_name)
		page.code = code
		page.title = title
		page.content = content
	except Page.DoesNotExist:
		page = Page(code=page_name, title=title, content=content)
	page.save()
	page_name = code
	return HttpResponseRedirect("/" + page_name + "/")

